package com.controller;

import com.dto.BaseDto;
import com.entity.Teacher;
import com.page.PageInfo;
import com.servcice.TeacherService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.swing.text.html.Option;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Controller
public class TeacherController extends BaseController{
    @Resource
    private TeacherService teacherService;

    @RequiresRoles("teacher")
    @RequestMapping("/teacher/getAllJSon")
    @ResponseBody //将返回值以json格式输出
    public BaseDto<Teacher> getAllJSon() throws Exception{
        return teacherService.getAll();
    }

    /**
     * 前台查询分页显示，ajax方式
     * @param pageInfo
     * @return
     */
    @RequestMapping("/teacher/page")
    @ResponseBody //将返回值以json格式输出
    public BaseDto<Teacher> getTeachersByPage(PageInfo pageInfo) throws Exception{

            String cacheId =  "teachersByPage";
        return teacherService.getTeachersByPage(pageInfo,cacheId);
    }

    /**
     * 前台 根据姓名搜索学生
     * @param name
     * @return
     */
        @RequestMapping("/teacher/searchTeacherByName/{name}")
        @ResponseBody //将返回值以json格式输出
        //@PathVariable 将url中以{xxx}形式传递的参数值 绑定给当前的方法某一个参数，就可以解决传递的参数名和方法定义的参数名 名称不同无法自动绑定的问题
        public BaseDto<Teacher> searchTeacherByName(@PathVariable("name") String name, HttpServletRequest request) throws Exception{
            String currPage = request.getParameter("currPage");
            String pageSize = request.getParameter("pageSize");
            PageInfo pageInfo = new PageInfo();
            currPage = Optional.ofNullable(currPage).orElse(String.valueOf(pageInfo.getCurrPage()));
            pageSize = Optional.ofNullable(pageSize).orElse(String.valueOf(pageInfo.getPageSize()));

            pageInfo.setCurrPage(Integer.parseInt(currPage));
            pageInfo.setPageSize(Integer.parseInt(pageSize));
            return teacherService.searchTeacherByName("%"+name+"%",pageInfo);
        }
    /**
     * 后台查询分页显示，跳转方式
     * @param pageInfo
     * @return
     */
    @RequestMapping("/teacher/back/page")
    public ModelAndView getTeachersBackByPage(PageInfo pageInfo) throws Exception{
        String cacheId =   "teachersByPage"+pageInfo.getCurrPage();//将页码拼接上，所以每一页都在redis有一个缓存
        BaseDto<Teacher> teachersDTO = teacherService.getTeachersByPage(pageInfo,cacheId);
        return new ModelAndView("/back/teacher/list","dto",teachersDTO);
    }



    /**
     * 前台根据学号（主键查询学生带选课信息）
     * @param sno
     * @return
     * @throws Exception
     */
    //restful风格  路径不能有动词  /teacher/back/teacher 表示按主键查询
    //                         /teacher/back/teachers  表示查询全部
    //                         /teacher/back/pageTeachers 表示分页查询
    //                         /teacher/back/
    @RequestMapping(value = "/front/teacherInfo/{sno}")
    public ModelAndView selectTeacherWithScsAndCourse(@PathVariable("sno") String sno) throws Exception{
        System.out.println("2222          "+sno);
        BaseDto<Teacher> teachersDTO = teacherService.selectTeacherWithScsAndCourse(sno);
        return new ModelAndView("/front/teacher/teacherInfo","dto",teachersDTO);
    }

    @RequestMapping(value = "/front/teacherInfoJson/{sno}")
    @ResponseBody
    public BaseDto selectTeacherWithScsAndCourse1(@PathVariable("sno") String sno) throws Exception{

        BaseDto<Teacher> teachersDTO = teacherService.selectTeacherWithScsAndCourse(sno);
        return teachersDTO;
    }






    @RequestMapping("/teacher/getAll")
    public ModelAndView getAll(){
        return new ModelAndView("/teacher/getAll");
    }

    @RequestMapping("/teacher/add")
    @ResponseBody
    public int addTeacher(Teacher teacher) throws Exception{
        return teacherService.addTeacher(teacher);
    }
}
